import { mapState, mapMutations } from 'vuex'
import { reverseDate,getNewToken,Trim } from '../../config/util'
import ordering from "../../components/ordering/ordering.vue"

export default {

	data() {
		return {
			ordering:false,//下单动画
			isParticulars: false,//是否报销
			insuranceList:[],//保险列表
			routerMess:[],//航班信息
			cabinMess:[],//舱位信息
			rules:null,//活动规则
			verify:false,//支付校验的弹框
			verifyText:'',//支付校验的提示
			showMore:false,//乘机人列表的展示与收起
			showMoreLimit:7,//乘机人列表展示限制数量
		}
	},
	computed: {
		...mapState([
			'isReimbursement', 'isInland', 'adultCount', 'passengerList', 'flight', 'phoneNumber', 'reimbursementMess',
			'insurance', 'cabin', 'isBack', 'price','contactUser','token'
		]),
		
		//结算总价格
		price_() {
			var price = 0;
			var count = this.choosedNameList.length;
			//票的价格
			var facePrice = this.orderMess.money * count;
			//基建燃油的价格			
			var otherFee = this.orderMess.otherFee * count;
			//税费			
			var tax = this.orderMess.tax * count;
			//保险的价格
			var insurance_ = 0;
			for(var i = 0; i < this.insurance_.length; i++) {
				insurance_ += this.insurance_[i].count * this.insurance_[i].insuranceFee;
			}
			//邮费的价格
			var postage = 0;
			if(this.isReimbursement) {
				var postage = this.postage;
			}
			price = facePrice + otherFee + insurance_ + postage + tax - this.activity_insurance - this.activity_ticket;
			return price?price:"---";
		},
		//票面立减
		activity_ticket(){	
			var rules = this.rules;
			var choosedNameList = this.choosedNameList;		
			if(rules.length==0) return 0;
			var totalMoney = 0;
			for(var i=0;i<rules.length;i++){
				if(rules[i].actContent==0){
					if(rules[i].passengerType == 0){
						totalMoney += Number(rules[i].actMoney)*choosedNameList.length;
					}else{
						for(var j=0;j<choosedNameList.length;j++){
							if(rules[i].passengerType == choosedNameList[j].passengerType){
								totalMoney += Number(rules[i].actMoney);
							}
						}
					}
				}		
			}
			return totalMoney;
		},
		//保险立减
		activity_insurance(){
			var that = this;
			var rules = this.rules;
			var choosedNameList = this.choosedNameList;			
			if(rules.length==0) return 0;
			var totalMoney = 0;
			//国际
			if(!this.isInland){
				if(rules[0].actContent==1){
					if(rules[0].passengerType == 0){
						totalMoney += Number(rules[0].actMoney)*insuranceCount()*routeCount();
					}else{
						for(var j=0;j<this.insurance.length;j++){
							for(var m=0;m<this.insurance[j].name.length;m++){
								if(this.insurance[j].name[m].isBuy){
									if(rules[0].passengerType==this.insurance[j].name[m].passengerType){
										totalMoney += Number(rules[0].actMoney)*routeCount();
									}
								}
							}
						}
					}
				}
				return totalMoney;
			}
			//国内
			for(var i=0;i<rules.length;i++){
				if(rules[i].actContent==1){
					if(rules[i].passengerType == 0){
						totalMoney += Number(rules[i].actMoney)*insuranceCount();
					}else{
						for(var j=0;j<this.insurance.length;j++){
							for(var m=0;m<this.insurance[j].name.length;m++){
								if(this.insurance[j].name[m].isBuy){
									if(rules[i].passengerType==this.insurance[j].name[m].passengerType){
										totalMoney += Number(rules[i].actMoney);
									}
								}
							}
						}
					}
				}		
			}
			return totalMoney;
			function insuranceCount(){
				var count = 0;
				for(var i=0;i<that.insurance_.length;i++){
					count += that.insurance_[i].count;
				}
				return count;
			}
			function routeCount(){
				return that.flight.inter.segments.length;
			}
		},
		//不同地区的邮费有差异
		postage() {
			var Area = ["内蒙古", "西藏", "海南", "宁夏", "青海", "新疆"];
			var province = this.reimbursementMess.address;
			var result = Area.some(function(item, index) {
				return province.indexOf(item) == 0
			})
			if(result) return 20;
			else return 10;
		},
		//已选保险的展示
		insurance_() {
			var that = this;
			var arr = [];
			for(var i = 0; i < this.insurance.length; i++) {
				var displayName = this.insurance[i].insurance.displayName;
				var insuranceFee = this.insurance[i].insurance.insuranceFee;
				var count = 0;
				for(var j = 0; j < this.insurance[i].name.length; j++) {
					if(this.insurance[i].name[j].isBuy) {
						count++;
					}
				}
				arr.push({
					"displayName": displayName, //保险名称
					"insuranceFee": insuranceFee*insuranceCount(), //保险费用
					"count": count //数量
				})
			}
			return arr;
			
			//保险段数
			function insuranceCount(){
				var count;
				if(that.isInland){
					count = 1;
					if(that.isBack) count = 2;
				}else{
					count = that.flight.inter.segments.length;
				}
				return count;
			}
		},
		//是否报销
		isReimbursement_: {
			get: function() {
				return this.isReimbursement;
			},
			set: function(newValue) {
				this.RECORD_ISREIMBURSEMENT(newValue);
			}
		},
		//联系手机
		phoneNumber_: {
			get: function() {
				return this.phoneNumber;
			},
			set: function(newValue) {
				this.RECORD_PHONENUMBER(newValue);
			}
		},
		//联系手机
		contactUser_: {
			get: function() {
				return this.contactUser;
			},
			set: function(newValue) {
				this.RECORD_CONTACTUSER(newValue);
			}
		},
		//选中的乘客
		choosedNameList() {
			var arr = [];
			for(var i = 0; i < this.passengerList.length; i++) {
				if(this.passengerList[i].checked != null) {
					arr.push(this.passengerList[i]);
				}
			}
			return arr;
		},
		//头部航班信息
		orderMess() {
			var orderMess = {};
			if(this.isInland) {
				orderMess.shijian = this.flight.inland[0].segments[0].fromDate;
				orderMess.flightNo = this.flight.inland[0].segments[0].flightNo;
				orderMess.fromAirCode = this.flight.inland[0].segments[0].fromAirport;
				orderMess.fromAir = this.flight.inland[0].segments[0].fromAirportCn;
				orderMess.fromTower = this.flight.inland[0].segments[0].fromTower;
				orderMess.toAir = this.flight.inland[0].segments[this.flight.inland[0].segments.length - 1].toAirportCn;
				orderMess.toAirCode = this.flight.inland[0].segments[this.flight.inland[0].segments.length - 1].toAirport;
				orderMess.toTower = this.flight.inland[0].segments[this.flight.inland[0].segments.length - 1].toTower;
				orderMess.money = 0;
				for(var i = 0; i < this.price.length; i++) {
					orderMess.money += this.price[i].adultFacePrice;
				}
				if(this.price.length == 1) {
					orderMess.otherFee = this.price[0].adultAirportTax + this.price[0].adultFuelTax || 0
					orderMess.tax = this.price[0].adultTax || 0
				} else {
					orderMess.otherFee = 0;
					orderMess.tax = 0;
					for(var i = 0; i < this.price.length; i++) {
						orderMess.otherFee += this.price[i].adultAirportTax + this.price[i].adultFuelTax || 0
						orderMess.tax += this.price[i].adultTax || 0
					}
				}
			} else {
				orderMess.shijian = this.flight.inter.segments[0].fromDate;
				orderMess.flightNo = this.flight.inter.segments[0].flightNo;
				orderMess.fromAir = this.flight.inter.segments[0].fromAirportCn;
				orderMess.fromTower = this.flight.inter.segments[0].fromTower;
				
				orderMess.money = this.price[0].adultFacePrice;
				orderMess.otherFee = this.price[0].adultAirportTax + this.price[0].adultFuelTax || 0;
				orderMess.tax = this.price[0].adultTax || 0;
				var rangeSegmentCount = this.flight.inter.rangeSegmentCount;
				var length = rangeSegmentCount.split(",").length;
				if(length==1){
					orderMess.toAir = this.flight.inter.segments[this.flight.inter.segments.length - 1].toAirportCn;
					orderMess.toTower = this.flight.inter.segments[this.flight.inter.segments.length - 1].toTower;
				}
				if(length==2){
					var leg = rangeSegmentCount.split(",")[0].split("-")[1];
					orderMess.toAir = this.flight.inter.segments[leg - 1].toAirportCn;
					orderMess.toTower = this.flight.inter.segments[leg - 1].toTower;
				}
			}
			return orderMess;
		}
	},
	methods: {
		...mapMutations([
			'RECORD_ISREIMBURSEMENT', 'RECORD_PASSENGERLIST', 'RECORD_ADULTCOUNT', 'RECORD_EDITPASSENGER', 'RECORD_PHONENUMBER', 'RECORD_REIMBURSEMENTMESS',
			'RECORD_INSURANCE','RECORD_CONTACTUSER','RECORD_TOKEN'
		]),
		flightDetail() {			
			this.$router.push({ path: '/orderDetail' });
		},
		toInsurance() {
			this.$router.push({ path: '/insurance' });
		},
		addPassenger() {
			this.$router.push({ path: '/addPassenger' });
		},
		editName(name) {
			this.RECORD_EDITPASSENGER(name);
			this.$router.push({ path: '/editPassenger' });
		},
		//航班明细的展开与收缩
		shrink(item,i){
			item.show = !item.show;			
		},
		//乘机人列表的收起与展开
		doShow(){
			if(this.showMore){
				this.showMoreLimit = 7;
			}else{
				this.showMoreLimit = Infinity;				
			}
			this.showMore = !this.showMore;
		},
		//选择乘机人
		checked(item, index) {
			if(item.checked == null) {
				this.limitPassengerCount(item, index);
			} else {
				//取消
				item.checked = null;
				this.count();
				this.deleteInsurancePassenger(item);
			}
		},
		//对乘机人人数做限制
		limitPassengerCount(item, index) {
			var count = this.getPassengerCount()+1;
			if(count>4){
				this.verify = true;
				this.verifyText = "乘机人数超过默认上限"
				return false;				
			}else{
				//乘机人数应该小于机票数量
				if(this.isInland&&this.isBack){
					let go = this.flight.RTCabin_detail[0].cabinList[0].seatCount;
					let back = this.flight.RTCabin_detail[1].cabinList[0].seatCount;
					let residue = Math.min.apply(null,[go,back]);
					if(residue<count){
						this.verify = true;
						this.verifyText = "机票不足"
						return false;
					}					
				}				
				if(this.isInland&&!this.isBack){					
					let residue = this.flight.OWCabin.cabinList[0].seatCount;
					if(residue<count){
						this.verify = true;
						this.verifyText = "机票不足"
						return false;	
					}
				}
			}
			//国内
			if(this.isInland) {
				item.checked = index;
				this.addInsurancePassenger(item);
				this.count()
				return false;
			}			
			//国际航班只能按照预定人数进行添加
			if(count < this.adultCount) {
				item.checked = index;
				this.addInsurancePassenger(item);
			}		
			
		},
		//添加乘客时 默认选中第一个保险
		addInsurancePassenger(item) {			
			if(this.insuranceList.length==0){
				return false;
			}
			var nameList = [];
			var mergeData = [];
			for(var i = 0; i < this.passengerList.length; i++) {
				if(this.passengerList[i].checked != null) {
					nameList.push(this.passengerList[i]);
				}
			}
			for(var i = 0; i < this.insuranceList.length; i++) {
				var obj = {};
				obj.insurance = this.insuranceList[i];
				obj.name = JSON.parse(JSON.stringify(nameList));
				//乘机人默认选中第一个保险
				if(i==0){
					for(var m=0;m<obj.name.length;m++){
						obj.name[m].isBuy = true;
					}
				}
				mergeData.push(obj);				
			}
			this.RECORD_INSURANCE(mergeData);
		},
		//选中保险后再次删除乘客 则删除store中对应的信息
		deleteInsurancePassenger(item) {
			if(this.insurance.length != 0) {
				this.RECORD_INSURANCE({ type: "delete", id: item.id })
			}
		},
		//更改store中的成人数量
		count() {
			var count = this.getPassengerCount();
			if(this.isInland){
				this.RECORD_ADULTCOUNT(count);
			}			
		},
		//获取已经选中的成人人数
		getPassengerCount() {
			var coun = 0;
			for(var i = 0; i < this.passengerList.length; i++) {
				if(this.passengerList[i].checked != null) {
					coun++
				}
			}
			return coun;
		},
		//删除选中的乘机人
		deleteName(name) {
			this.deleteInsurancePassenger(name);
			name.checked = null;
			this.count()
		},
		//支付提交数据
		getPayData() {
			var that = this;
			var postParams = [];
			//国内往返
			if(this.isBack && this.isInland) {
				var inland = this.flight.inland;
				var price = this.flight.RTCabin_detail;
				for(var i = 0; i < inland.length; i++) {
					var data = {
						pltmOrder: getPltmOrder(price[i], that.price[i], i),//订单信息
						pltmOrderFlights: getPltmOrderFlights(inland[i],price[i]),//各航节信息
						pltmOrderPassengerList: getPltmOrderPassengerList(),//乘客信息
						flightType: this.isInland ? "1" : "2", //国际国内
						flightRangeType: (!this.isInland && this.isBack) ? "RT" : "OW", //行程类型
						insureOrders: getInsureOrders(inland[i]) //保险信息				
					};
					postParams.push(data);
				}
			} 
			else {
				var flight = null;
				if(this.isInland) flight = this.flight.inland[0];
				else flight = this.flight.inter;
				var price = this.flight.OWCabin;

				var data = {
					pltmOrder: getPltmOrder(price, that.price[0]),//订单信息
					pltmOrderFlights: getPltmOrderFlights(flight,price),//各航节信息
					pltmOrderPassengerList: getPltmOrderPassengerList(),//乘客信息
					flightType: this.isInland ? "1" : "2", //国际国内
					flightRangeType: (!this.isInland && this.isBack) ? "RT" : "OW", //行程类型
					insureOrders: getInsureOrders(flight) //保险信息				
				};
				//postParams.push(data);
				postParams = data;
			}
			//获取乘机人信息
			function getPltmOrderPassengerList(){
				var result = [];
				var passengerList = that.passengerList;
				for(var i=0;i<passengerList.length;i++){
					if(passengerList[i].checked!=null){
						passengerList[i].passageType = passengerList[i].passengerType;
						passengerList[i].lastName_cn = passengerList[i].lastNameCn||passengerList[i].firstNameCn;
						result.push(passengerList[i])
					}
				}
				return result;
			}
			//获取各航节信息
			function getPltmOrderFlights(flight,price) {
				var result = [];
				for(var i = 0; i < flight.segments.length; i++) {
					var obj = {
						"arrivalCity": flight.segments[i].toAirportCn,
						"arrivalTerminal": flight.segments[i].toTower,
						"arrivalDate": (flight.segments[i].toDate).substr(0,10),
						"arrivalTime": (flight.segments[i].toDate).substr(-5),
						"cabin": price.cabinList[i].cabinRank,
						"cabinCode": price.cabinList[i].cabinCode,
						"departureCity": flight.segments[i].fromAirportCn,
						"departureTerminal": flight.segments[i].fromTower,
						"departureDate": (flight.segments[i].fromDate).substr(0,10),
						"departureTime": (flight.segments[i].fromDate).substr(-5),
						"flightId": flight.segments[i].realFlightNo,
						"shareFlightNo": flight.segments[i].shareFlightNo,
						"flightSegment": null,//分情况获取
						"departureCode": flight.segments[i].fromAirport,
						"arrivalCode": flight.segments[i].toAirport,
						"subPrdId": null,
						"stopoverInfo": flight.segments[i].stopOver,
						"flightDuration": flight.segments[i].flightDuration,
						"flightNumber": flight.segments[i].flightNo,
						"planeModel": flight.segments[i].planeModel,
						"connectInfoid": -1
					}
					if(that.isBack&&!that.isInland){
						var segsCount = flight.rangeSegmentCount.split(",")[0].split("-")[1];
						if(i<segsCount){
							obj.flightSegment = 1;
						}else{
							obj.flightSegment = 2;
						}						
					}else{
						var flightSegment = flight.rangeSegmentCount.split(",")[0].split("-")[0];
						obj.flightSegment = flightSegment;
					}
					result.push(obj);
				}
				return result;
			}
			//获取订单信息
			function getPltmOrder(price, realPrice, i) {
				var obj = {};
				obj.supplier = "6";
				obj.supplierName = price.supplierId;
				obj.routingType = (!that.isInland && that.isBack) ? "RT" : "OW"; //行程类型
				obj.unitPrice = realPrice.adultFacePrice || 0;
				obj.adultPrice = realPrice.adultFacePrice || 0;
				obj.childPrice = realPrice.childFacePrice || 0;
				obj.infantPrice = realPrice.infantFacePrice || 0;
				obj.adultTFC = realPrice.adultAirportTax || 0 + realPrice.adultFuelTax || 0 + realPrice.adultTax || 0;
				obj.childTFC = realPrice.childAirportFee || 0 + realPrice.childFuelFee || 0 + realPrice.childTax || 0;
				obj.infantTFC = realPrice.infantAirportFee || 0 + realPrice.infantFuelFee || 0 + realPrice.infantTax || 0;
				obj.adultSettle = realPrice.adultSettlePrice || 0;
				obj.childSettle = realPrice.childSettlePrice || 0;
				obj.infantSettle = realPrice.infantSettlePrice || 0;
				obj.airportFeeAdult = realPrice.adultAirportTax || 0;
				obj.airportFeeChild = realPrice.childAirportFee || 0;
				obj.airportFeeInfant = realPrice.infantAirportFee || 0;
				obj.fuelTaxAdult = realPrice.adultFuelTax || 0;
				obj.fuelTaxChild = realPrice.childFuelFee || 0;
				obj.fuelTaxInfant = realPrice.infantFuelFee || 0;
				obj.seatClass = price.cabinList[0].cabinRank;
				obj.toStartAirport = ""; //这几个sb参数在下面分情况获取
				obj.toEndAirport = ""; //这几个sb参数在下面分情况获取
				obj.toStartDate = ""; //这几个sb参数在下面分情况获取
				obj.adultCount = that.adultCount;
				obj.childCount = 0;
				obj.infantCount = 0;
				obj.policyID = price.policy[0].policyId;
				obj.policyIdChd = price.policy[0].policyIdChd;
				
				obj.settlement = realPrice.adultSettlePrice || 0;
				obj.contactUser = that.contactUser;
				obj.contactMobile = that.phoneNumber;
				obj.contactEmail = "aljh@yixiangyunda.com";
				obj.isReimburse = that.isReimbursement ? 1 : 0;
				//邮寄费用只收一段
				obj.mailFee = 0;
				if(i!=undefined&&i==0&&that.isReimbursement_){
					obj.mailFee = that.postage;
				}
				if(i==undefined&&that.isReimbursement_){
					obj.mailFee = that.postage;
				}
				
				obj.receiver = that.reimbursementMess.name;
				obj.receiverPhone = that.reimbursementMess.phone;
				obj.receiverAddress = that.reimbursementMess.address;
				obj.receiverCity = '';
				//国内单程
				if(that.isInland && !that.isBack) {
					var inland = that.flight.inland[0];
					obj.toStartAirport = inland.segments[0].fromAirport;
					obj.toEndAirport = inland.segments[inland.segments.length - 1].toAirport;
					obj.toStartDate = inland.segments[0].fromDate;
				}
				//国内往返
				if(that.isInland && that.isBack) {
					var inland = that.flight.inland[i];
					obj.toStartAirport = inland.segments[0].fromAirport;
					obj.toEndAirport = inland.segments[inland.segments.length - 1].toAirport;
					obj.toStartDate = inland.segments[0].fromDate;
				}
				//国际
				if(!that.isInland) {
					obj.freightLimitQueryID = price.freightLimitQueryID;
					obj.freightLimitQueryIDChd = price.freightRuleQueryIDChd;
					var inter = that.flight.inter;
					var segs = inter.rangeSegmentCount.split(",").length;
					if(segs == 1) { //国际单程
						obj.toStartAirport = inter.segments[0].fromAirport;
						obj.toEndAirport = inter.segments[inter.segments.length - 1].toAirport;
						obj.toStartDate = inter.segments[0].fromDate;
					}
					if(segs == 2) { //国际往返
						var legs = inter.rangeSegmentCount.split(",")[0].split("-")[1];
						obj.toStartAirport = inter.segments[0].fromAirport;
						obj.toEndAirport = inter.segments[legs - 1].toAirport;
						obj.toStartDate = inter.segments[0].fromDate;
					}
				}
				return obj;
			}
			//保险信息			
			function getInsureOrders(flight) {
				//var index = flight.rangeSegmentCount.split(",")[0].split("-")[1];
				var segments = flight.segments;
				var result = [];				
				for(var m=0;m<segments.length;m++){
					for(var i = 0; i < that.insurance.length; i++) {
						for(var j = 0; j < that.insurance[i].name.length; j++) {
							var obj = {};
							if(that.insurance[i].name[j].isBuy) {
								obj.holderName = that.insurance[i].name[j].firstNameCn;
								obj.holderIdentityType = that.insurance[i].name[j].docType;
								obj.holderIdentityNo = that.insurance[i].name[j].docId;
								obj.holderSexType = that.insurance[i].name[j].sex;
								obj.holderBirthday = that.insurance[i].name[j].birthDate;
								obj.holderTelephone = that.insurance[i].name[j].mobile;
								obj.flightNo = segments[m].flightNo;
								obj.flightDate = (segments[m].fromDate).substr(0,10);
								obj.originCode = segments[m].fromAirport;
								obj.destinationCode = segments[m].toAirport;
								obj.policyNo = that.insurance[i].insurance.id;
								result.push(obj);
							}
						}
					}
				}
				return result;
			}
			return postParams;
		},
		//点击去支付
		toPay() {
			//报销凭证信息校验
			if(this.isReimbursement_&&(this.reimbursementMess.name==''||this.reimbursementMess.phone==''||this.reimbursementMess.address=='')){
				this.verify = true;
				this.verifyText = '请详细填写报销凭证信息';
				return false;
			}
			//选中乘机人校验
			if(this.choosedNameList.length==0){
				this.verify = true;
				this.verifyText = '请选择乘机人';
				return false;
			}	
			//联系人校验
			if(Trim(this.contactUser)==''||Trim(this.phoneNumber)==''){
				this.verify = true;
				this.verifyText = '请填写联系人信息';
				return false;
			}
			
			this.ordering = true;
			var url = "";
			if(this.isInland&&this.isBack) url = '/order/createRT';
			else url = '/order/create';
			
			this.$axios({
					method: 'post',
					url: url,
					data: this.getPayData(),
					headers: {'authorization':this.token}
				})
				.then((res) => {
					
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});
					this.payResultHandle(res)
				})
				.catch((error) => {
					this.ordering = false;
					alert("订单创建失败")
				})
		},
		payResultHandle(res){			
			var success = res.data.success;
			if(success){
				var orderId = res.data.obj;
				this.pay(orderId)		
				
			}else{
				this.ordering = false;
				alert(res.data.msg)
			}
		},
		//下单成功后调支付接口
		pay(id){
			this.$axios.post('/pay/pre?temp=' + Math.random(), {
						orderNumber:id
					},
					{
						headers: {'authorization':this.token}
					}
				)
				.then((res) => {
					this.ordering = false;
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});	
					var status = res.data.status;
					if(status==1001) {
						var url = res.data.resultInfo;
						window.location.href = url;				
					}
					else{
						alert("支付失败")
					}
				})
				.catch((error) => {
					this.ordering = false;
					alert(error)
				})
		},
		//点击明细
		particulars() {
			var count = this.choosedNameList.length;
			if(!count) return false;
			this.isParticulars = !this.isParticulars;
		},
		//通知页面
		tongzhi1() {
			this.$router.push({ path: '/DGRinformation' });
		},
		tongzhi2() {
			this.$router.push({ path: '/bookingInformation' });
		},
		//请求乘机人列表
		getPassenger() {
			this.$axios.get('/passenger/query?temp=' + Math.random(), {
					params: { flightType: this.isInland ? "1" : "2" },
					headers: {'authorization':this.token}
				})
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});
					if(res.data.success) this.addStatus(res);
					else alert("乘客列表请求失败");
				})
		},
		//向请求到的数据中添加字段
		addStatus(res) {
			for(var i = 0; i < res.data.obj.rows.length; i++) {
				res.data.obj.rows[i].checked = null;
			}
			this.RECORD_PASSENGERLIST({ type: "getAll", passenger: res.data.obj.rows });
		},
		//保险列表
		getInsurance(){
			this.$axios.get('/insurance/query?temp=' + Math.random(), {
					headers: {'authorization':this.token}
				})
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});
					this.insuranceList = res.data;
				})
				.catch((error) => {

				})
		},
		//总时长
		flightDuration(value) {
			var duration = 0;
			for(var i = 0; i < value.length; i++) {
				duration += value[i].flightDuration;
			}
			return duration;
		},
		//中转停留时间
		stopTime(seg, index, segments) {
			if(segments[index + 1]) {
				var toDate = seg.toDate;
				var fromDate = segments[index + 1].fromDate;
				var min = (reverseDate(fromDate) - reverseDate(toDate)) / 60000;
				return min;
			}
		},
		/*判断是否经停*/
		stopOver(flight){
			var directFlight = flight.directFlight;
			if(!directFlight) return {show:false};//优先展示中转
			var length = flight.segments[0].stopOver.length;
			if(length==0) return {show:false};
			if(length == 1){
				return {show:true,city:flight.segments[0].stopOver[0].stopAirport}
			}else if(length == 2){
				return {show:true,city:'2次',color:true}
			}else{
				return {show:true,city:'多次'}
			}	
		},
		/*判断是否中转*/
		directFlight(flight){			
			var rangeSegmentCount = flight.rangeSegmentCount;
			var segmentCount = rangeSegmentCount.split(",")[0].split("-")[1];
			if(segmentCount == 1){
				return {show:false}
			}else if(segmentCount == 2){
				return {show:true,city:flight.segments[0].toCityCn}
			}else if(segmentCount == 3){
				return {show:true,city:'2次',color:true}
			}else{
				return {show:true,city:'多次'}
			}			
		},		
		//舱位信息
		cabinDetail(i,item){						
			var rangeSegmentCount = item.rangeSegmentCount;
			var segCount = rangeSegmentCount.split("-")[1];
			var cabinList = this.cabinMess[i].cabinList;
			var result = [];
			for(var j=0;j<segCount;j++){
				result.push(cabinList[j].cabinRank);
			}
			return result;		
		},
		//退改签规定
		endorse() {
			this.$router.push({ path: '/endorse' });
		},
		//处理航班信息
		getRouter(){			
			//国际处理
			if(!this.isInland){
				var inter = this.flight.inter;
				var segments = JSON.parse(JSON.stringify(inter.segments));
				var rangeSegmentCount = JSON.parse(JSON.stringify(inter.rangeSegmentCount)).split(",");
				for(var i = 0; i < rangeSegmentCount.length; i++) {
					this.routerMess.push(JSON.parse(JSON.stringify(inter)));
					var legs = rangeSegmentCount[i].split("-")[1];
					this.routerMess[i].segments = segments.splice(0, legs);					
					this.routerMess[i].rangeSegmentCount = rangeSegmentCount[i];
				}
			}else{
				this.routerMess = JSON.parse(JSON.stringify(this.flight.inland))				
			}
			
			//增加显示隐藏字段
			for(var i=0;i<this.routerMess.length;i++){
				this.$set(this.routerMess[i],'show',false); 			
			}
			
			/*舱位信息处理*/
			//国内往返
			if(this.isBack&&this.isInland){
				this.cabinMess = this.flight.RTCabin_detail;				
			}
			//国际往返
			else if(!this.isInland&&this.isBack){
				var cabinMess = JSON.parse(JSON.stringify(this.flight.OWCabin));
				var inter = this.flight.inter;
				var rangeSegmentCount = JSON.parse(JSON.stringify(inter.rangeSegmentCount)).split(",");
				for(var i = 0; i < rangeSegmentCount.length; i++) {
					this.cabinMess.push(JSON.parse(JSON.stringify(cabinMess)));
					var legs = rangeSegmentCount[i].split("-")[1];
					this.cabinMess[i].cabinList = cabinMess.cabinList.splice(0, legs);				
				}
			}else{
				this.cabinMess = [this.flight.OWCabin];								
			}	
		},
		//优惠规则
		activity(){
			var rules = [];
			for(var i=0;i<this.routerMess.length;i++){				
				if(this.routerMess[i].activity){
					var rule = {
						actContent : this.routerMess[i].activity.actContent,
						actMoney : this.routerMess[i].activity.actMoney,
						passengerType : this.routerMess[i].activity.passengerType
					};	
					rules.push(rule);
				}				
			}	
			this.rules = rules;
		}
	},
	components:{
		ordering
	},
	created() {	
		document.title = "预订";
		this.getPassenger();
		this.getInsurance();
		this.getRouter();
		this.activity();
	}
}